
import time
from datetime import datetime, timedelta
# time 模块


def my_time():
    timestamp = time.time()  # 获取时间戳
    mylocaltime = time.localtime()
    print(f'当前时间戳是:{timestamp}')
    print(f'获取当前时间:{mylocaltime}')

    # time.struct_time -> str
    mystr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print(f'time.struct_time -> str:{mystr}')

    # str -> time.struct_time
    print(f'str -> time.struct_time -> str:{time.strptime(mystr, "%Y-%m-%d %H:%M:%S")}')

    # time.struct_time -> tuple
    my_tuple = tuple(time.localtime())
    print(f'tuple:{my_tuple}')

    # tuple -> time.struct_time
    print(f'tuple -> time.struct_time:{time.struct_time(my_tuple)}')

    # timestamp -> time.struct_time 时间戳转换成当前时间
    my_struct_time = time.localtime(time.time())  # 提供了有时区的接口
    print(f'timestamp -> time.struct_time:{my_struct_time}')
    my_gmtime = time.gmtime(time.time())   # 没有时区接口
    print(f'timestamp -> time.my_gmtime:{my_gmtime}')

    # time.struct_time -> timestamp 当前时间转换成时间戳
    my_mktime = time.mktime(my_struct_time)
    print(f'time.struct_time -> timestamp 当前时间转换成时间戳:{my_mktime}')

# 时间运算  只能通过时间格式 -> 元组，然后创建新元组来新建一个新的时间


def my_datetime():
    print(datetime.today())
    print(datetime.now(tz=None))  # 可以指定时区,默认是None
    print(datetime.utcnow())  # 返回Utc时间

    # datetime.datetime -> str
    # 此处并不像time模块一样用模块调用，而是用datetime.datetime对象来转换[datetime.datetime].strftime("%Y-%m-%d %H:%M:%S")
    today_time = datetime.today()
    my_datetime_str = today_time.strftime("%Y-%m-%d %H:%M:%S")
    print(f'datetime -> str：{my_datetime_str}')

    # str -> datetime.datetime
    my_newdatetime = datetime.strptime(my_datetime_str, "%Y-%m-%d %H:%M:%S")
    print(f'str -> datetime.datetime:{my_newdatetime}')

    # datetime.datetime -> timetuple -> tuple
    tt = today_time.timetuple()
    tuple_datetime = tuple(tt)
    print(f'datetime -> timetuple -> tuple：{tuple_datetime}')

    # tuple -> datetime.datetime
    my_tuple = tuple_datetime[:-3]
    print(f'tuple -> datetime：{datetime(*my_tuple)}')  # *是传入不定长的元组

    # datetime.datetime -> timestamp 时间戳
    my_timestamp = today_time.timestamp()
    print(f'datetime -> timestamp:{my_timestamp}')

    # timestamp -> datetime.datetime
    new_datetime = datetime.fromtimestamp(my_timestamp)
    print(f'timestamp -> datetime:{new_datetime}')

    # 时间运算
    print(f'修改前的时间:{today_time}')
    new_revise_time = today_time + timedelta(seconds=2.0, microseconds=2.0, milliseconds=3.0,
                                             minutes=30.0, hours=6.0, days=5.0)

    print(f'修改后的时间:{new_revise_time}')


# 封装一个函数get_date(days) , 通过传入的数值days(days=1[-2,3,4,5]), 返回格式化后的时间
def get_date(days):
    try:
        day = float(days)
    except Exception as e:
        raise ParamsTypeError('请输入数字：{}'.format(e.args))
    else:
        now = datetime.today()
        time_result = now + timedelta(days=float(day))
        return time_result
    finally:
        pass


# 自定义异常
class ParamsTypeError(Exception):
    pass


if __name__ == '__main__':
    my_time()
    my_datetime()
    myday = input('请输入你要计算的时间:')
    result = get_date(myday)
    print(f'你要计算的时间结果为：{result}')
